package util;

import java.sql.*;
import java.io.*;
import java.util.*;


public class DBUtil {
	
    public Statement 	stmt 	= null;
    public Connection 	conn 	= null;
    public ResultSet 	rset 	= null;
    public String		message	= "";
    
    public DBUtil() {}
    
    public void connect() {
	
		Properties props = new Properties();
	    try {
	      Debug.log("Loading props");
	      Debug.log(System.getProperty("dbPath"));
	      FileInputStream dbprops = new FileInputStream(System.getProperty("dbPath") + "WEB-INF/classes/util/db.prop");
	      props.load(dbprops); 
	    }
	    catch( IOException ie ) {
	      System.err.println(ie.getMessage());
	      Debug.log(ie.getMessage());
	    }
	   
	    String dbUrl 	= props.getProperty("db.url");
	    String dbDriver = props.getProperty("db.driver");
	    String login 	= props.getProperty("db.user");
	    String passwd 	= props.getProperty("db.pwd");
	    	
	    try {
	    	Debug.log("Loading driver " + dbDriver);
	        Class.forName(dbDriver);
	        conn = DriverManager.getConnection(dbUrl, login, passwd);
	        conn.setAutoCommit(false);
	    }
	    catch( ClassNotFoundException ce ) {
	    	ce.printStackTrace();
	    	Debug.log(ce.getMessage());
	    }
	    catch( SQLException se ) {
	    	Debug.log(se.getMessage());
	    	Debug.loge(se);
	        message += se.getMessage();
	    }
    }
    
    public void executeStmt (String sqlstmt) {
        try {
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rset = stmt.executeQuery(sqlstmt);
        }
        catch( SQLException se ) {
            message += se.getMessage();
            Debug.log(se.getMessage());
            Debug.loge(se);
        }
    }
    
    public void close() {
    	try {
	    	//stmt.close();
	    	//rset.close();
	    	conn.close();
    	}
    	catch( SQLException se ) {
    		message += se.getMessage();
    		Debug.log(se.getMessage());
    		Debug.loge(se);
    	}
    }
}